<template>
	<view class="maintenance">
		<toSafe :bgColor="'#fff'"></toSafe>
		<view class="searchInput">
			<view class="search-top">
				<view class="icon">
					<uni-icons type="left" size="30" @click="HandleReturn" style="margin-right: 20rpx;"></uni-icons>
				</view>
				<view class="title" @click="show=true">{{title}}</view>
			</view>
			<button class="top-btn" @click="seeDirectory" v-if="isRoles">查看目录</button>
		</view>
		<up-loadmore status="loading" v-if="isLoading" />
		<scroll-view :scroll-y="true" class="updata-form" v-else-if="isRoles&&!NewForm">
			<up-empty mode="data" icon="http://cdn.uviewui.com/uview/empty/data.png"
				v-if="!form.contentList || form.contentList?.length == 0">
			</up-empty>
			<view class="control-room" v-for="item in form.contentList" :key="item.id">
				<text class="control-title">{{item.name}}</text>
				<text class="control-message">{{item.intro??'暂无简介'}}</text>
				<view class="control-info">
					<view class="position">
						<view class="label">
							<text>检查部位</text>
							<text>具体位置精确到楼层</text>
						</view>
						<up-input v-model="item.checkPoint" placeholder="请输入" clearable border='none'
							customStyle="background-color:#F6F8FA;height: 64rpx;"></up-input>
					</view>
					<view class="message">
						<view class="label">检查情况及处理说明</view>
						<up-textarea v-model="item.wbexplain" placeholder="请输入"
							customStyle="background-color:#F6F8FA;"></up-textarea>
					</view>
					<view class="result">
						<view class="label">
							结果判定
						</view>
						<!-- up-input v-model="item.isOk" placeholder="请输入合格或不合格" clearable border='none'
							customStyle="background-color:#F6F8FA;">
						</up-input> -->
						<up-radio-group v-model="item.result" placement="row">
							<up-radio :customStyle="{marginBottom: '8px'}" label="无" :name="3"
								style="margin-right: 30rpx;">
							</up-radio>
							<up-radio :customStyle="{marginBottom: '8px'}" label="合格" :name="1"
								style="margin-right: 30rpx;">
							</up-radio>
							<up-radio :customStyle="{marginBottom: '8px'}" label="不合格" :name="2">
							</up-radio>
							
		
						</up-radio-group>
					</view>
				</view>
			</view>
		</scroll-view>
		<scroll-view :scroll-y="true" class="updata-formq" v-else-if='isRoles&&NewForm'>
			<uni-forms ref="from1" :modelValue="model1" :rules="rulesfrom" label-position="top" label-width="300rpx">
				<uni-forms-item label="其他" class="form-item-vertical" name="rests">
					<up-textarea v-model="model1.rests" placeholder="请输入"
						customStyle="background-color:#fafcfe;"></up-textarea>
				</uni-forms-item>
				<uni-forms-item label="培训" class="form-item-vertical" name="train">
					<up-textarea v-model="model1.train" placeholder="请输入"
						customStyle="background-color:#fafcfe;"></up-textarea>
				</uni-forms-item>
				<uni-forms-item label="问题汇总" class="form-item-vertical" name="collect" required>
					<up-textarea v-model="model1.collect" placeholder="请输入"
						customStyle="background-color:#fafcfe;"></up-textarea>
				</uni-forms-item>
				<uni-forms-item label="更换材料清单" class="form-item-vertical" name="inventory" required>
					<up-textarea v-model="model1.inventory" placeholder="请输入"
						customStyle="background-color:#fafcfe;"></up-textarea>
				</uni-forms-item>
				<uni-forms-item label="建议方案" class="form-item-vertical" name="scheme" required>
					<up-textarea v-model="model1.scheme" placeholder="请输入"
						customStyle="background-color:#fafcfe;"></up-textarea>
				</uni-forms-item>
			</uni-forms>
		</scroll-view>
		<!-- 预览图片 -->
		<scroll-view class="proview" :scroll-y="true" v-else>
			<up-loading-page :loading="loading" bg-color="#e8e8e8" loading-text="加载中..."></up-loading-page>
			<image :src="imgSrc" mode="widthFix" class="image"></image>
		</scroll-view>

		<!-- 底部按钮 -->
		<view class="fire-buttom">
			<view class="up-page" @click="upPage" v-if="isRoles">
				<!-- <up-icon size="40" name="@/static/image/上一页.png"></up-icon> -->
				<image src="@/static/image/uppage.png" mode=""></image>
				<text>上一页</text>
			</view>
			<view class="down-page" @click="downPage" v-if="isRoles">
				<!-- <up-icon size="40" name="@/static/image/下一页.png"></up-icon> -->
				<image src="@/static/image/downpage.png" mode=""></image>
				<text>下一页</text>
			</view>
			<view class=""></view>
			<button class="submit" @click="submit" v-if="isRoles">提交</button>
			<button class="submit" @click="check" v-else>审核</button>
			<button type="button" @click="previewHandler" v-if="isRoles">预览</button>
			<button class="load-receipt1" @click="loadReceipt" v-if="isRoles">上传凭条</button>
			<button class="load-receipt1" @click="loadReceipt" v-else>查看凭条</button>
		</view>
		<!-- 预览 -->
		<!-- view class="float-preview">
			<button type="button" @click="previewHandler">预览</button>
	</view> -->
		<u-modal :show="show" :content='title' @confirm="confirm"></u-modal>
		<up-popup :show="isShow" :closeOnClickOverlay="false" :round="10" mode="bottom">
			<view class="popup">
				<view class="popup-top">
					<view class="top-left" @click="coles">
						取消
					</view>
					<view class="top-right" @click="popupSubmit">
						确认
					</view>
				</view>
				<view class="popup-body">
					<uni-forms ref="valiForm" :rules="rules" :modelValue="checkForm" :label-width="80"
						label-align="right">
						<uni-forms-item label="是否合格" name="test" style="display: flex;align-items: center;">
							<uni-data-checkbox v-model="checkForm.test" :localdata="tests" style="line-height: 100%;" />
						</uni-forms-item>
						<uni-forms-item label="描述" name="introduction">
							<uni-easyinput type="textarea" v-model="checkForm.auditRemark" placeholder="请输入审核结果描述" />
						</uni-forms-item>
					</uni-forms>
				</view>
			</view>
		</up-popup>

	</view>
</template>

<script setup>
	import {
		getCatalogueList,
		submitForm,
		planAudit,
		checkReject,
		updatePlanForm,
		wbPlanByIdGet
	} from "@/api/maintenance.js"
	import {
		previewMonthly
	} from "@/api/proview.js"
	import toSafe from '@/compoents/top-safe.vue'
	import EventBus from '@/utils/eventBus.js'
	import {
		onLoad,
		onShow,
	} from '@dcloudio/uni-app'
	import {
		ref,
		reactive,
		onMounted,
		getCurrentInstance
	} from 'vue'
	const {
		proxy
	} = getCurrentInstance()
	let form = reactive({})
	let dataLists = reactive([])
	const isLoading = ref(false)
	const NewForm = ref(false)
	let itemId = ref()
	let isId = ref()
	let isRoles = ref(true)
	let isShow = ref(false)
	let show = ref(false)
	let checkForm = reactive({
		test: 0,
		auditRemark: '',
	})
	const title = ref('')
	let tests = reactive([{
		text: '合格',
		value: 0
	}, {
		text: '不合格',
		value: 1
	}])
	let rules = reactive({})
	let testStatus = ref()
	// 建筑信息列表
	const tableData = reactive([{
		text: "A", //显示
		value: "" // 值
	}])
	//预览图片
	const imgSrc = ref("")
	const loading = ref(false)
	const from1 = ref(null);
	const model1 = ref({
		rests: '',
		train: '',
		collect: '',
		inventory: '',
		scheme: ''
	})
	const rulesfrom = reactive({
		// rests: {
		// 	rules: [{
		// 		type: 'string',
		// 		required: true,
		// 		errorMessage: '请填写其他',
		// 		trigger: ['blur', 'change'],
		// 	}]
		// },
		// train: {
		// 	rules: [{
		// 		type: 'string',
		// 		required: true,
		// 		errorMessage: '请输入培训',
		// 		trigger: ['blur', 'change'],
		// 	}]
		// },
		collect: {
			rules: [{
				type: 'string',
				required: true,
				errorMessage: '请输入问题汇总',
				trigger: ['change'],
			}]
		},
		inventory: {
			rules: [{
				type: 'string',
				required: true,
				errorMessage: '请输入更新材料清单',
				trigger: ['change'],
			}]
		},
		scheme: {
			rules: [{
				type: 'string',
				required: true,
				errorMessage: '请输入建议方案',
				trigger: ['change'],
			}]
		}
	})
	const indexValue = ref(0)
	onMounted(() => {})
	onLoad(options => {
		console.log(options,'options');
		itemId.value = options.itemId
		isId.value = options.id
		isRoles.value = options.type == '审核' ? false : true
		if (options.indexValue) {
			indexValue.value = options.indexValue
			NewForm.value = true
			title.value = '反馈'
			// downPage()
			getList()
		} else {
			testStatus.value = options.status
			title.value = options.name
			if (options.name) {
				getMonthImg()
				getList()
			} else {
				getMonthImg()
				getList()
				getPage()
			}
		}
		wbPlanByIdGetFun()

		// getRoles()
	})
	const getRoles = () => {
		let roles = uni.getStorageSync('roles')
		roles.split(',').forEach(item => {
			if (item == '一级消防师' || item == '工程师') {
				isRoles.value = false
			} else {
				isRoles.value = true
			}
		})
	}
	const getList = () => {
		isLoading.value = true
		getCatalogueList(itemId.value).then(res => {
			if (res.code == 200) {
				if (!title.value) {
					title.value = res.rows[0]?.classifyName
				}
				res.rows.forEach(item => {
					item.contentList.forEach(i => {
						i.classifyId = item.classifyId
						i.result = 3
					})
				})
				dataLists.splice(0, dataLists.length, ...res.rows)
				console.log(dataLists, 'dataLists');
				getPage(isId.value)
			}
			isLoading.value = false
		}).catch(err => {
			isLoading.value = false
		})
	}
	const confirm = () => {
		show.value = false;
	}
	const getPage = (id = null) => {
		if (id) {
			dataLists?.forEach(item => {
				if (item.classifyId == id) {
					getForm(item)
				}
			})
			isLoading.value = false
		} else {
			getForm(dataLists[0])
		}
	}
	// 判断是否合格
	const getForm = item => {
		// const item = JSON.parse(uni.getStorageSync('form_Item'))
		// item?.contentList?.map(i => {
		// 	i.isOk = i.result == 1 ? '合格' : '不合格'
		// })
		Object.assign(form, item)
	}
	// 上一页
	const upPage = () => {

		if (NewForm.value) {
			NewForm.value = false
			console.log(dataLists, 'getList()');
			dataLists.forEach((item, index) => {
				if (form.classifyId !== item.classifyId) return
				if (index == 0) {
					uni.showToast({
						title: '已经是第一页了！',
						icon: 'none',
					})
					title.value = dataLists[index].classifyName
				} else {
					title.value = dataLists[index].classifyName
					Object.assign(form, dataLists[index])
				}
			})
		} else {
			dataLists.forEach((item, index) => {
				if (form.classifyId !== item.classifyId) return
				if (index == 0) {
					uni.showToast({
						title: '已经是第一页了！',
						icon: 'none',
					})
				} else {
					title.value = dataLists[index - 1].classifyName
					Object.assign(form, dataLists[index - 1])
				}
			})
		}

		uni.pageScrollTo({
		    scrollTop: 0, // 滚动到页面的顶部
		    duration: 300 // 滚动动画的时长，单位ms
		});
		
		
		// EventBus.emit('updata-page', form.index - 1)
		// getForm()
	}

	// 下一页
	const downPage = () => {
		
	

		if (!NewForm.value) {
			const matchingItem = dataLists.find(item => item.classifyId === form.classifyId);
			if (matchingItem) {
				const nextIndex = dataLists.indexOf(matchingItem) + 1;
				if (nextIndex < dataLists.length) {
					title.value = dataLists[nextIndex].classifyName
					Object.assign(form, dataLists[nextIndex]);
				} else {
					title.value = '反馈'
					NewForm.value = true
				}
			}
		} else {
			uni.showToast({
				title: '已经是最后一页了！',
				icon: 'none',
			});
		}
		
		uni.pageScrollTo({
		    scrollTop: 0, // 滚动到页面的顶部
		    duration: 300 // 滚动动画的时长，单位ms
		});

		// EventBus.emit('updata-page', form.index + 1)
		// getForm()
	}

	const HandleReturn = () => {
		// uni.navigateBack({
		// 	delta: 1
		// });
		if (isRoles.value) {
			uni.redirectTo({
				url: '/pages/maintenance/tasks'
			})

		} else {
			uni.redirectTo({
				url: '/pages/maintenance/check'
			})

		}

	}

	//反馈回显
	const wbPlanByIdGetFun = () => {
		wbPlanByIdGet(itemId.value).then(res => {
			console.log(res, itemId.value, 'qqqqqq');
			model1.value = {
				rests: res.data.rests,
				train: res.data.train,
				collect: res.data.collect,
				inventory: res.data.inventory,
				scheme: res.data.scheme
			}

		})

	}

	// 查看目录
	const seeDirectory = () => {
		uni.navigateTo({
			url: `/pages/catalogue/index?id=${itemId.value}&type=${isRoles.value?'':'审核'}`
		})
	}

	// 提交任务
	const submit = () => {
		if (NewForm.value) {
			proxy.$refs.from1.validate().then(res => {
				uni.showLoading({
					title: '任务提交中'
				})
				updatePlanForm({
					...model1.value,
					id: itemId.value
				}).then(res => {
					uni.showToast({
						title: "提交成功"
					})
				})
			})
			return
		}
		uni.showLoading({
			title: '任务提交中'
		})
		let data = []
		dataLists?.forEach(i => {
			i.contentList?.forEach(item => {
				item.planId = itemId.value
				if (item.result) {
					data.push({
						...item,
						result: item.result == 1 ? '合格' : '不合格'
					})
				} else {
					data.push(item)
				}
			})
		})
		console.log(data);
		submitForm(data).then(res => {
			uni.hideLoading();
			if (res.code == 200) {
				uni.showToast({
					title: '任务提交成功！'
				})
			}
		})
	}
	// 上传/查看凭条
	const loadReceipt = () => {
		if (isRoles.value) {
			if (!model1.value.collect || !model1.value.scheme || !model1.value.inventory) {
				uni.showToast({
					title: '请先填写提交反馈！',
					icon: 'none'
				})
			} else {
				updatePlanForm({
					...model1.value,
					id: itemId.value
				}).then(res => {
					uni.navigateTo({
						url: `/pages/photo/updata?id=${itemId.value}&isRoles=${isRoles.value}&status=${testStatus.value}`
					})
				})

			}
			return
		}
		uni.navigateTo({
			url: `/pages/photo/updata?id=${itemId.value}&isRoles=${isRoles.value}&status=${testStatus.value}`
		})
	}

	// 审核
	const check = () => {
		isShow.value = true
	}
	const popupSubmit = () => {
		let form = {
			planId: itemId.value,
			status: testStatus.value
		}
		Object.assign(form, checkForm)
		if (checkForm.test == 0) {
			planAudit(form).then(res => {
				if (res.code == 200) {
					uni.showToast({
						title: '审核成功',
						icon: 'none'
					});
					isShow.value = false
				}
				// uni.navigateTo({
				// 	url: '/pages/maintenance/check'
				// })
				uni.switchTab({
					url: '/pages/index/index'
				})
			})
		} else {
			checkReject(form).then(res => {
				if (res.code == 200) {
					uni.showToast({
						title: '审核成功',
						icon: 'none'
					});
					Object.assign(checkForm, {
						test: 0,
						auditRemark: '',
					})
				}
				uni.switchTab({
					url: '/pages/index/index'
				})
			})
		}
	}
	const coles = () => {
		isShow.value = false
		Object.assign(checkForm, {
			test: 0,
			auditRemark: '',
		})
	}
	const previewHandler = () => {
		uni.navigateTo({
			url: `/pages/proview/index?id=${itemId.value}`
		})
	}
	// 获取月检任务
	const getMonthImg = () => {
		loading.value = true
		previewMonthly(itemId.value).then(res => {
			loading.value = false
			getData(res)
		})
	}
	const getData = res => {
		if (res.code == 200) {
			imgSrc.value = res.msg
		}
	}
</script>

<style lang="less" scoped>
	@import "../../App.less";

	// ::v-deep.uni-forms-item {
	// 	margin-bottom: 20rpx;
	// }

	::v-deep.uni-forms-item.is-direction-top .uni-forms-item__label {
		font-weight: 600;
		font-size: 26rpx;
		color: #000;
	}

	.maintenance {
		width: 100%;
		height: 100%;
		background: #F6F8FA;
		overflow: hidden;

		.searchInput {
			width: 100%;
			height: 88rpx;
			display: flex;
			flex-direction: column;
			justify-content: space-around;
			background-color: #fff;

			// position: fixed;
			// top: 0;
			// left: 0;
			padding: 0 20rpx;
			z-index: 99;

			.search-top {
				.inline_center();
				height: 88rpx;
				width: 100%;
				// display: flex;
				// align-items: center;

				.icon {
					width: 10vw;
					height: 88rpx;
					line-height: 88rpx;
					z-index: 1;
				}

				.title {
					width: 90vw;
					line-height: 88rpx;
					height: 88rpx;
					text-align: center;
					margin-left: -8vw;
					font-size: 34rpx;
					font-weight: 700;
					overflow: hidden;
					text-overflow: ellipsis;
					white-space: nowrap;
				}

			}

			.top-btn {
				position: absolute;
				right: 28rpx;
				// width: 160rpx;
				height: 64rpx;
				line-height: 64rpx;
				background-color: #325EF5;
				color: #fff;
				font-size: 28rpx
			}

			.search_icons {
				.inline_center();
				width: 100%;
				height: 68rpx;
				padding: 16rpx 20rpx;
				background-color: #F2F2F2;
				border-radius: 34rpx;

				.icon {
					margin-right: 10rpx;
				}

				.uni-input {
					width: 100%;
					border-right: 1px solid #325EF5;
					margin-right: 20rpx;
					height: 100%;
				}

				.search {
					background-color: inherit;
					width: 100rpx;
					height: 68rpx;
					font-size: 32rpx;
					color: #325EF5;
					line-height: 68rpx;
				}
			}

		}

		.updata-form {
			padding: 0 28rpx;
			height: calc(100vh - 200rpx);

			.control-room {
				display: flex;
				flex-direction: column;
				padding-bottom: 50rpx;

				.control-title {
					font-size: 28rpx;
					font-weight: bold;
				}

				.control-message {
					padding: 10rpx 0;
					font-size: 24rpx;
					opacity: 0.6;
				}

				.control-info {
					padding: 20rpx;
					background-color: #fff;
					border-radius: 12rpx;

					.position {
						.label {
							margin-bottom: 10rpx;

							text:nth-child(2) {
								font-size: 24rpx;
								opacity: 0.6;
								margin-left: 20rpx;
							}
						}
					}

					.message {
						margin-top: 20rpx;

						.label {
							font-size: 28rpx;
							margin-bottom: 10rpx;
						}
					}

					.result {
						margin-top: 20rpx;

						.label {
							font-size: 28rpx;
							margin-bottom: 10rpx;
						}

						.result-check {
							display: flex;

							>view {
								flex: 1;
							}
						}
					}
				}

				&:nth-child(1) {
					margin-top: 20rpx;
				}

				&::after {
					content: '';
					display: block;
					width: 100%;
					height: 20rpx;
				}
			}


		}

		.proview {
			width: 100%;
			height: calc(100vh - 200rpx);

			// overflow: hidden;
			.image {
				width: 100%;
				height: 100%;
			}
		}

		.updata-formq {
			padding: 0 28rpx;
			height: calc(100vh - 300rpx);

			.control-room {
				display: flex;
				flex-direction: column;
				padding-bottom: 50rpx;

				.control-title {
					font-size: 28rpx;
					font-weight: bold;
				}

				.control-message {
					padding: 10rpx 0;
					font-size: 24rpx;
					opacity: 0.6;
				}

				.control-info {
					padding: 20rpx;
					background-color: #fff;
					border-radius: 12rpx;

					.position {
						.label {
							margin-bottom: 10rpx;

							text:nth-child(2) {
								font-size: 24rpx;
								opacity: 0.6;
								margin-left: 20rpx;
							}
						}
					}

					.message {
						margin-top: 20rpx;

						.label {
							font-size: 28rpx;
							margin-bottom: 10rpx;
						}
					}

					.result {
						margin-top: 20rpx;

						.label {
							font-size: 28rpx;
							margin-bottom: 10rpx;
						}

						.result-check {
							display: flex;

							>view {
								flex: 1;
							}
						}
					}
				}

				&:nth-child(1) {
					margin-top: 20rpx;
				}

				&::after {
					content: '';
					display: block;
					width: 100%;
					height: 20rpx;
				}
			}
		}

		.fire-buttom {
			width: 100%;
			height: 112rpx;
			position: absolute;
			bottom: 0;
			padding: 9rpx 28rpx;
			background-color: #fff;
			display: flex;
			justify-content: space-between;
			align-items: center;
			box-shadow: 0 0 20rpx 0 #cacaca;

			.up-page {
				display: flex;
				flex-direction: column;
				align-items: center;
				font-size: 24rpx;

				>image {
					width: 40rpx;
					height: 40rpx;
				}
			}

			.down-page {
				display: flex;
				flex-direction: column;
				align-items: center;
				font-size: 24rpx;

				>image {
					width: 40rpx;
					height: 40rpx;
				}
			}

			>button {
				width: 175rpx;
				height: 58rpx;
				background-color: #325EF5;
				color: #fff;
				border-radius: 16rpx;
				margin: 0;
				line-height: 58rpx;
				font-size: 25rpx;
			}
		}


		.search-placeholder {
			width: 100%;
			height: 60rpx;
			background-color: #cfd9f9;
			font-size: 28rpx;
			color: #325EF5;
			text-align: center;
			line-height: 60rpx;
		}


		.uni-list {
			height: 80rpx;
		}
	}

	.uni-group {
		display: flex;
		align-items: center;
	}

	.popup {
		height: 50vh;
		display: flex;
		flex-direction: column;

		.popup-top {
			padding: 10rpx 28rpx;
			font-size: 30rpx;
			color: #325EF5;
			background-color: #F2F2F2;
			.inline_space_between()
		}

		.popup-body {
			flex: 1;
			padding: 0 28rpx;
			margin-top: 20rpx;
			// background-color: #dadada;
		}
	}

	.float-preview {
		position: fixed;
		right: 20rpx;
		bottom: 150rpx;

		button {
			border-radius: 10%;
			background-color: #325EF5;
			color: #fff;
		}
	}
</style>